import instance from "./api"

// 登录接口响应类型
interface LoginResponse {
    setAccessToken: string;
    setRefreshToken: string;
    user: {
      _id: string;
      username: string;
      roleid?: string;
      [key: string]: any;
    };
    message?: string;
  }

  // 登录接口参数类型
  interface LoginParams {
    username: string;
    password: string;
  }

  export function LoginAPI(user: LoginParams): Promise<LoginResponse> {
    return instance({
      url: "/login",
      method: 'POST',
      data: user
    });
  }

// 人脸识别登录
export interface FaceLoginResponse {
    code: number;
    msg: string;
    data?: {
        FaceId: string;
        Gender: number;
        PersonId: string;
        PersonName: string;
    };
}

export function faceLogin(base64: string): Promise<FaceLoginResponse> {
    return instance({
        url: "/faceLogin",
        method: 'POST',
        timeout: 60000, // 60秒超时
        headers: {
            'Content-Type': 'application/json'
        },
        data: {
            base64
        }
    }).then(response => {
        if (!response || typeof response !== 'object') {
            throw new Error('Invalid response format');
        }

        const faceLoginResponse = response as any;
        if (typeof faceLoginResponse.code !== 'number' || typeof faceLoginResponse.msg !== 'string') {
            throw new Error('Invalid response format');
        }

        return faceLoginResponse as FaceLoginResponse;
    });
}

export function List(){
    return instance({
        url: "/list",
    })
}

export function add(user:object){
    return instance({
        url: "/add",
        method:'POST',
        data:user
    })
}

// 人脸录入接口
export interface FaceAddResponse {
    success: boolean;
    msg: string;
    error?: string;
    message?: string;
}

export function faceAdd(base64: string, name: string, id: string): Promise<FaceAddResponse> {
    return instance({
        url: "/faceadd",
        method: 'POST',
        timeout: 60000, // 60秒超时
        headers: {
            'Content-Type': 'application/json'
        },
        data: {
            base64,
            name,
            id
        }
    });
}
// 文件信息类型
export interface FileInfo {
    title: string;
    url: string;
    fileHash: string;
    uploadDate: string;
}

// 获取文件列表
export interface FileListResponse {
    code: number;
    message: string;
    data: FileInfo[];
}

export function getFileList(): Promise<FileListResponse> {
    return instance({
        url: "/files",
        method: "GET"
    });
}

// 检查文件是否存在
export function checkFileExists(fileHash: string): Promise<boolean> {
    return instance({
        url: `/check-file?filehash=${fileHash}`,
        method: "HEAD"
    })
        .then(() => true)
        .catch(() => false);
}

// 上传分片
export interface UploadResponse {
    progress: number;
    title: string;
}

export function uploadFile(formData: FormData): Promise<UploadResponse> {
    return instance({
        url: "/upload",
        method: "POST",
        headers: {
            "Content-Type": "multipart/form-data"
        },
        data: formData
    });
}

// 合并分片
export interface MergeParams {
    filename: string;
    totalChunks: number;
    title: string;
    fileHash: string;
    idd: string
}

export interface MergeResponse {
    code: number;
    message: string;
}

export function mergeFile(params: MergeParams): Promise<MergeResponse> {
    return instance({
        url: "/merge",
        method: "POST",
        data: params
    });
}

// 网络测速
export function speedTest(): Promise<Blob> {
    return instance({
        url: "/speed-test",
        method: "GET",
        responseType: "blob"
    });
}
// 添加动态数据接口
export interface AddDynamicResponse {
    code: number;
    msg: string;
}

export function addDynamic(data: any): Promise<AddDynamicResponse> {
    return instance({
        url: "/addDynamic",
        method: 'POST',
        data: { data }
    });
}

// 删除动态数据接口
export interface DeleteDynamicResponse {
    code: number;
    msg: string;
}

export function deleteDynamic(data: any): Promise<DeleteDynamicResponse> {
    return instance({
        url: "/deleteDynamic",
        method: 'POST',
        data: { data }
    });
}
export interface AdvInfo {
    _id: string;
    title: string;
    url: string;
    q: {
        _id?: string;
        guan: string;
        img: string;
        username?: string;
    };
}

// 广告列表响应类型
export interface AdvListResponse {
    code: number;
    message?: string;
    data: AdvInfo[];
}
export function getAdvList(): Promise<AdvListResponse> {
    return instance({
        url: "/advs",
        method: "GET"
    });
}
// 关注/取消关注接口参数类型
export interface ToggleFollowParams {
    advId: string;
}

// 关注/取消关注响应类型
export interface ToggleFollowResponse {
    code: number;
    message: string;
    data: {
        guan: string;
    };
}

export function toggleFollow(params: ToggleFollowParams): Promise<ToggleFollowResponse> {
    return instance({
        url: "/toggleFollow",
        method: "POST",
        headers: {
            'Content-Type': 'application/json'
        },
        data: params
    });
}
